package com.jzo2o.orders.dispatch.strategys;

import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.orders.dispatch.model.dto.ServeProviderDTO;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Mr.M
 * @version 1.0
 * @description 距离优先规则
 * @date 2023/12/30 16:04
 */
//public class DistanceRule implements IProcessRule {
public class DistanceRule extends AbstractProcessRule {

    //距离优先规则的下一个规则
//    private IProcessRule next;

    public DistanceRule(IProcessRule next){
        super(next);
//        this.next = next;
    }

    //根据距离排序，找到最近的师傅
    public List<ServeProviderDTO> doFilter(List<ServeProviderDTO> serveProviderDTOS){

        if(CollUtils.isEmpty(serveProviderDTOS)){
            return Collections.emptyList();
        }
        //根据距离排序
        List<ServeProviderDTO> collect = serveProviderDTOS.stream().sorted(Comparator.comparing(ServeProviderDTO::getAcceptanceDistance)).collect(Collectors.toList());
        //拿到最近的师傅
        ServeProviderDTO first = CollUtils.getFirst(collect);
        //从list中找和最近师傅的距离相同的师傅
        List<ServeProviderDTO> collect1 = collect.stream().filter(v -> v.getAcceptanceDistance().compareTo(first.getAcceptanceDistance()) == 0).collect(Collectors.toList());

        return collect1;
    }

//    @Override
//    public List<ServeProviderDTO> filter(List<ServeProviderDTO> serveProviderDTOS) {
//        //根据距离排序，找到最近的师傅
//        List<ServeProviderDTO> result = doFilter(serveProviderDTOS);
//
//        //如果找到了多个师傅，继续匹配下一个规则
//        if(CollUtils.isNotEmpty(result) && result.size()>1 && next!=null){
//            return next.filter(result);
//        }else{
//            //当前没有匹配到或只匹配到一个师傅，直接返回，不再向下匹配规则了
//            return result;
//        }
//    }
//
//    @Override
//    public IProcessRule next() {
//        return next;
//    }
}
